package com.yourcompany.api.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yourcompany.api.entity.RolePermission;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface RolePermissionMapper extends BaseMapper<RolePermission> {

    /**
     * 获取角色的权限ID列表
     */
    @Select("SELECT permission_id FROM role_permission WHERE role_id = #{roleId} AND is_deleted = 0")
    List<Long> getPermissionIdsByRoleId(@Param("roleId") Long roleId);

    /**
     * 获取多个角色的权限ID列表
     */
    @Select("<script>" +
            "SELECT DISTINCT permission_id FROM role_permission " +
            "WHERE role_id IN " +
            "<foreach collection='roleIds' item='roleId' open='(' separator=',' close=')'>" +
            "#{roleId}" +
            "</foreach>" +
            " AND is_deleted = 0" +
            "</script>")
    List<Long> getPermissionIdsByRoleIds(@Param("roleIds") List<Long> roleIds);

    /**
     * 批量插入角色权限关联
     */
    @Insert("<script>" +
            "INSERT INTO role_permission (role_id, permission_id, creator_id, create_time) VALUES " +
            "<foreach collection='rolePermissions' item='item' separator=','>" +
            "(#{item.roleId}, #{item.permissionId}, #{item.creatorId}, now())" +
            "</foreach>" +
            "</script>")
    int insertBatch(@Param("rolePermissions") List<RolePermission> rolePermissions);
} 